package com.sugar.follow.up.module.business.controller.admin.publicityeducation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.publicityeducation.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.publicityeducation.PublicityEducationDO;
import com.sugar.follow.up.module.business.service.publicityeducation.PublicityEducationService;

@Tag(name = "管理后台 - 宣教")
@RestController
@RequestMapping("/business/publicity-education")
@Validated
public class PublicityEducationController {

    @Resource
    private PublicityEducationService publicityEducationService;

    @PostMapping("/create")
    @Operation(summary = "创建宣教")
    @PreAuthorize("@ss.hasPermission('business:publicity-education:create')")
    public CommonResult<Long> createPublicityEducation(@Valid @RequestBody PublicityEducationSaveReqVO createReqVO) {
        return success(publicityEducationService.createPublicityEducation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新宣教")
    @PreAuthorize("@ss.hasPermission('business:publicity-education:update')")
    public CommonResult<Boolean> updatePublicityEducation(@Valid @RequestBody PublicityEducationSaveReqVO updateReqVO) {
        publicityEducationService.updatePublicityEducation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除宣教")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:publicity-education:delete')")
    public CommonResult<Boolean> deletePublicityEducation(@RequestParam("id") Long id) {
        publicityEducationService.deletePublicityEducation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除宣教")
                @PreAuthorize("@ss.hasPermission('business:publicity-education:delete')")
    public CommonResult<Boolean> deletePublicityEducationList(@RequestParam("ids") List<Long> ids) {
        publicityEducationService.deletePublicityEducationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得宣教")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:publicity-education:query')")
    public CommonResult<PublicityEducationRespVO> getPublicityEducation(@RequestParam("id") Long id) {
        PublicityEducationDO publicityEducation = publicityEducationService.getPublicityEducation(id);
        return success(BeanUtils.toBean(publicityEducation, PublicityEducationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得宣教分页")
    @PreAuthorize("@ss.hasPermission('business:publicity-education:query')")
    public CommonResult<PageResult<PublicityEducationRespVO>> getPublicityEducationPage(@Valid PublicityEducationPageReqVO pageReqVO) {
        PageResult<PublicityEducationDO> pageResult = publicityEducationService.getPublicityEducationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PublicityEducationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出宣教 Excel")
    @PreAuthorize("@ss.hasPermission('business:publicity-education:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPublicityEducationExcel(@Valid PublicityEducationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PublicityEducationDO> list = publicityEducationService.getPublicityEducationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "宣教.xls", "数据", PublicityEducationRespVO.class,
                        BeanUtils.toBean(list, PublicityEducationRespVO.class));
    }

}